package com.campus.controller;

import com.campus.domain.dto.*;
import com.campus.domain.vo.*;
import com.campus.service.ILostItemService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 失物招领Controller
 */
@RestController
@RequestMapping("/lost")
@Slf4j
public class LostItemController {
    
    @Resource
    private ILostItemService lostItemService;
    
    /**
     * 上传图片
     */
    @PostMapping("/upload/image")
    public Result<UploadImageVO> uploadImage(@RequestParam("file") MultipartFile file) throws Exception {
        log.info("上传图片：{}", file.getOriginalFilename());
        UploadImageVO vo = lostItemService.uploadImage(file);
        return Result.ok(vo);
    }
    
    /**
     * 发布失物
     */
    @PostMapping("/publish")
    public Result<Long> publishLostItem(@RequestParam("title") String title,
                                        @RequestParam("description") String description,
                                        @RequestParam("location") String location,
                                        @RequestParam("foundTime") String foundTime,
                                        @RequestParam("category") String category,
                                        @RequestParam("images") String[] images) {
        log.info("发布失物：title={}, category={}", title, category);
        Long id = lostItemService.publishLostItem(title, description, location, foundTime, category, images);
        return Result.ok(id);
    }
    
    /**
     * 获取失物列表
     */
    @GetMapping("/list")
    public Result<PageResultVO<LostItemVO>> getLostItemList(LostItemQueryDTO queryDTO) {
        log.info("查询失物列表：{}", queryDTO);
        PageResultVO<LostItemVO> result = lostItemService.getLostItemList(queryDTO);
        return Result.ok(result);
    }
    
    /**
     * 获取失物详情
     */
    @GetMapping("/detail/{id}")
    public Result<LostItemVO> getLostItemDetail(@PathVariable Long id) {
        log.info("查询失物详情：id={}", id);
        LostItemVO vo = lostItemService.getLostItemDetail(id);
        return Result.ok(vo);
    }
    
    /**
     * 更新失物
     */
    @PutMapping("/update/{id}")
    public Result<Void> updateLostItem(@PathVariable Long id,
                                       @RequestParam("title") String title,
                                       @RequestParam("description") String description,
                                       @RequestParam("location") String location,
                                       @RequestParam("foundTime") String foundTime,
                                       @RequestParam("category") String category,
                                       @RequestParam("images") String[] images) {
        log.info("更新失物：id={}", id);
        lostItemService.updateLostItem(id, title, description, location, foundTime, category, images);
        return Result.ok();
    }
    
    /**
     * 删除失物
     */
    @DeleteMapping("/delete/{id}")
    public Result<Void> deleteLostItem(@PathVariable Long id) {
        log.info("删除失物：id={}", id);
        lostItemService.deleteLostItem(id);
        return Result.ok();
    }
    
    /**
     * 点赞失物
     */
    @PostMapping("/like/{itemId}")
    public Result<Void> likeItem(@PathVariable Long itemId) {
        log.info("点赞失物：itemId={}", itemId);
        lostItemService.likeItem(itemId);
        return Result.ok();
    }
    
    /**
     * 取消点赞失物
     */
    @DeleteMapping("/unlike/{itemId}")
    public Result<Void> unlikeItem(@PathVariable Long itemId) {
        log.info("取消点赞失物：itemId={}", itemId);
        lostItemService.unlikeItem(itemId);
        return Result.ok();
    }
    
    /**
     * 申领失物
     */
    @PostMapping("/claim")
    public Result<Void> claimItem(@RequestBody ClaimDTO claimDTO) {
        log.info("申领失物：{}", claimDTO);
        lostItemService.claimItem(claimDTO);
        return Result.ok();
    }
    
    /**
     * 添加评论
     */
    @PostMapping("/comment/add")
    public Result<Long> addComment(@RequestBody CommentDTO commentDTO) {
        log.info("添加评论：{}", commentDTO);
        Long id = lostItemService.addComment(commentDTO);
        return Result.ok(id);
    }
    
    /**
     * 获取评论列表
     */
    @GetMapping("/comment/list")
    public Result<PageResultVO<CommentVO>> getCommentList(@RequestParam Long itemId,
                                                          @RequestParam(defaultValue = "1") Integer page,
                                                          @RequestParam(defaultValue = "10") Integer pageSize) {
        log.info("查询评论列表：itemId={}, page={}, pageSize={}", itemId, page, pageSize);
        PageResultVO<CommentVO> result = lostItemService.getCommentList(itemId, page, pageSize);
        return Result.ok(result);
    }
    
    /**
     * 删除评论
     */
    @DeleteMapping("/comment/delete/{commentId}")
    public Result<Void> deleteComment(@PathVariable Long commentId) {
        log.info("删除评论：commentId={}", commentId);
        lostItemService.deleteComment(commentId);
        return Result.ok();
    }
    
    /**
     * 点赞评论
     */
    @PostMapping("/comment/like/{commentId}")
    public Result<Void> likeComment(@PathVariable Long commentId) {
        log.info("点赞评论：commentId={}", commentId);
        lostItemService.likeComment(commentId);
        return Result.ok();
    }
    
    /**
     * 取消点赞评论
     */
    @DeleteMapping("/comment/unlike/{commentId}")
    public Result<Void> unlikeComment(@PathVariable Long commentId) {
        log.info("取消点赞评论：commentId={}", commentId);
        lostItemService.unlikeComment(commentId);
        return Result.ok();
    }
    
    /**
     * 添加回复
     */
    @PostMapping("/reply/add")
    public Result<Long> addReply(@RequestBody ReplyDTO replyDTO) {
        log.info("添加回复：{}", replyDTO);
        Long id = lostItemService.addReply(replyDTO);
        return Result.ok(id);
    }
    
    /**
     * 删除回复
     */
    @DeleteMapping("/reply/delete/{replyId}")
    public Result<Void> deleteReply(@PathVariable Long replyId) {
        log.info("删除回复：replyId={}", replyId);
        lostItemService.deleteReply(replyId);
        return Result.ok();
    }
    
    /**
     * 点赞回复
     */
    @PostMapping("/reply/like/{replyId}")
    public Result<Void> likeReply(@PathVariable Long replyId) {
        log.info("点赞回复：replyId={}", replyId);
        lostItemService.likeReply(replyId);
        return Result.ok();
    }
    
    /**
     * 取消点赞回复
     */
    @DeleteMapping("/reply/unlike/{replyId}")
    public Result<Void> unlikeReply(@PathVariable Long replyId) {
        log.info("取消点赞回复：replyId={}", replyId);
        lostItemService.unlikeReply(replyId);
        return Result.ok();
    }
    
    /**
     * 获取待审核申领列表（管理员）
     */
    @GetMapping("/claims/pending")
    @PreAuthorize("@ex.hasAuthority('lost:audit')")
    public Result<PageResultVO<ClaimAuditVO>> getPendingClaims(@RequestParam(defaultValue = "1") Integer page,
                                                                @RequestParam(defaultValue = "10") Integer pageSize) {
        log.info("获取待审核申领列表：page={}, pageSize={}", page, pageSize);
        PageResultVO<ClaimAuditVO> vo = lostItemService.getPendingClaims(page, pageSize);
        return Result.ok(vo);
    }
    
    /**
     * 审核申领（管理员）
     */
    @PostMapping("/claims/audit")
    @PreAuthorize("@ex.hasAuthority('lost:audit')")
    public Result auditClaim(@RequestBody AuditClaimDTO auditDTO) {
        log.info("审核申领：claimId={}, status={}", auditDTO.getClaimId(), auditDTO.getStatus());
        lostItemService.auditClaim(auditDTO);
        return Result.ok();
    }
}

